﻿using Consul;
using DouDou.Shop.AuthorizationCenter.JWT.Utility;
using DouDou.Shop.Common.Models;
using DouDou.Shop.DTOModel.DTO;
using DouDou.Shop.WebCore.JWTExtend;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace MengLin.Shopping.AuthorizationCenter.JWT.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthorizationCenterController : ControllerBase
    {
        private ILogger<AuthorizationCenterController> _iLogger = null;
        private IJWTService _iJWTService = null;
        private readonly IConfiguration _iConfiguration = null;
        private HttpHelperService _httpHelperService = null;
        

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="iLoggerFactory"></param>
        /// <param name="ilogger"></param>
        /// <param name="iConfiguration">注入配置</param>
        /// <param name="iJWTservice">注入jwt服务</param>
        public AuthorizationCenterController(ILoggerFactory iLoggerFactory,
            ILogger<AuthorizationCenterController> ilogger,
            IConfiguration iConfiguration,
            IJWTService iJWTservice,
            HttpHelperService httpHelperService)
        {
            _iLogger = ilogger;
            _iConfiguration = iConfiguration;
            _iJWTService = iJWTservice;
            _httpHelperService = httpHelperService;
        }

        /// <summary>
        /// 【根据用户名和密码查询用户是否合法，验证成功==>颁发Token】
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>token</returns>
        [Route("Login")]
        [HttpPost]
        public string Login(string? userName, string? password)
        {
            string requestUrl = $"{_iConfiguration["VerifyUserUrl"]}?userName={userName}&password={password}";
            //这里先模拟验证用户成功
            //AjaxResult<DTOJWTUser> ajaxResult = new AjaxResult<DTOJWTUser>();
            //ajaxResult.Result = true;
            AjaxResult<DTOJWTUser> ajaxResult = _httpHelperService.VerifyUser(requestUrl);
            if (ajaxResult.Result)
            {
                var dtoUser = ajaxResult.TValue;

                string token = _iJWTService.GetToken(new JWTUserModel()
                {
                    Id = 1,
                    Name = "1",
                    Account = "menglin",
                    EMail = "menglin2010@126.com",
                    Mobile_Phone = "15319998282",
                    Sex = 1,
                    Age = 39,
                    Role = "Admin"//角色授权
                });
                ajaxResult.Value = token;
            }
            else
            {
                return JsonConvert.SerializeObject(new
                {
                    result = false,
                    jwt = ""
                });
            }
            Console.WriteLine($"Accredit Result : {JsonConvert.SerializeObject(ajaxResult)}");
            return JsonConvert.SerializeObject(ajaxResult);
        }

        /// <summary>
        /// 【验证通过网关OcelotGeteway讲请求转发到这里】
        /// </summary>
        /// <returns></returns>
        [Route("TestOcelotGeteway")]
        [HttpGet]
        public string TestOcelotGeteway()
        {
            return "123456";
        }
    }
}
